<?php
define('SABLOG_ROOT', dirname(__FILE__));
$onoff = function_exists('ini_get') ? ini_get('register_globals') : get_cfg_var('register_globals');
if ($onoff != 1) {
	@extract($_POST, EXTR_SKIP);
	@extract($_GET, EXTR_SKIP);
}
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];

require_once(SABLOG_ROOT.'/include/func/global.func.php');

if ($action == 'first' || $action == 'second') {
	// 允许程序在 register_globals = off 的环境下工作
	// 加载数据库配置信息
	require_once(SABLOG_ROOT.'/config.php');
	// 加载数据库类
	require_once(SABLOG_ROOT.'/include/class/mysql.class.php');
	// 初始化数据库类
	$DB = new DB_MySQL;
	$DB->connect($servername, $dbusername, $dbpassword, $dbname, $usepconnect);
	unset($servername, $dbusername, $dbpassword, $dbname, $usepconnect);

	$step = (!$step) ? 1 : $step;
	$a = (!$a) ? 0 : $a;
	$percount = ($percount <= 0) ? 500 : $percount;
	$start    = ($step - 1) * $percount;
	$next     = $start + $percount;
	$step++;
	$jumpurl = $php_self.'?action='.$action.'&step='.$step.'&percount='.$percount;
	$goon = 0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SABLOG-X | Powered by 4ngel</title>
<link href="install.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if ($action == 'first') {
	$query = $DB->query("SELECT commentid,url FROM {$db_prefix}comments WHERE url<>'' ORDER BY commentid LIMIT ".$start.", ".$percount);
	while ($comment = $DB->fetch_array($query)) {
		$goon = 1;
		if (isemail($comment['url'])) {
			$DB->unbuffered_query("UPDATE {$db_prefix}comments SET email=url, url='' WHERE commentid='".$comment['commentid']."'");
		}
	}
	echo '<div class="install_main">';
	if($goon) {
		echo '<p class="p2">评论数据正在更新 '.$start.' 到 '.$next.' 项</p><p class="p2"><a href="'.$jumpurl.'">程序将自动跳转.如果没有自动跳转,请点击这里.</a></p>';
		echo '<meta HTTP-EQUIV="REFRESH" content="3;URL='.$jumpurl.'">';
	} else {
		echo '<p class="p2">成功重建所有评论数据</p><p class="p2"><a href="?action=second">程序将自动跳转.如果没有自动跳转,请点击这里.</a></p>';
		echo '<meta HTTP-EQUIV="REFRESH" content="3;URL=?action=second">';
	}
	echo '</div></body></html>';
	exit;
} elseif ($action == 'second') {
	$query = $DB->query("SELECT userid,url FROM {$db_prefix}users WHERE url<>'' ORDER BY userid LIMIT ".$start.", ".$percount);
	while ($user = $DB->fetch_array($query)) {
		$goon = 1;
		if (isemail($user['url'])) {
			$DB->unbuffered_query("UPDATE {$db_prefix}users SET email=url, url='' WHERE userid='".$user['userid']."'");
		}
	}
	echo '<div class="install_main">';
	if($goon) {
		echo '<p class="p2">用户数据正在更新 '.$start.' 到 '.$next.' 项</p><p class="p2"><a href="'.$jumpurl.'">程序将自动跳转.如果没有自动跳转,请点击这里.</a></p>';
		echo '<meta HTTP-EQUIV="REFRESH" content="3;URL='.$jumpurl.'">';
	} else {
		echo '<p class="p2">成功重建所有用户数据</p><p class="p2"><a href="?action=three">程序将自动跳转.如果没有自动跳转,请点击这里.</a></p>';
		exit;
		echo '<meta HTTP-EQUIV="REFRESH" content="3;URL=?action=three">';
	}
	echo '</div></body></html>';
	exit;
} else {
?>
<div class="install_main">
  <div id="install_innertext">
    <p class="p2">感谢您选择由 <a href="http://www.4ngel.net/" target="_blank">安全天使网络安全小组</a> 开发的 <a href="http://www.4ngel.net/">SaBlog-X</a> 博客程序!</p>
    <p class="p2">当前版本为 <u>SaBlog-X v 1.6</u></p>
    <p class="p2">目标版本为 <u>SaBlog-X v 2.0</u></p>
    <p class="p2">升级过程完全不用人工干预,请耐心等待成功带来的喜悦.</p>
	<p class="p2"><a href="<?=$php_self?>?action=first">升级数据</a></p>
  </div>
</div>
<?php
}
?>
<div class="copyright">Powered by SaBlog-X (C) 2003-2006 Security Angel Team</div>
</body>
</html>
